package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDatasource;
import com.ruoyi.system.service.ISysDatasourceService;

/**
 * 数据源配置Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/datasource")
public class SysDatasourceController extends BaseController
{
    @Autowired
    private ISysDatasourceService datasourceService;

    /**
     * 获取数据源配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDatasource datasource)
    {
        startPage();
        List<SysDatasource> list = datasourceService.selectDatasourceList(datasource);
        return getDataTable(list);
    }

    /**
     * 导出数据源配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:export')")
    @GetMapping("/export")
    public AjaxResult export(SysDatasource datasource)
    {
        List<SysDatasource> list = datasourceService.selectDatasourceList(datasource);
        ExcelUtil<SysDatasource> util = new ExcelUtil<SysDatasource>(SysDatasource.class);
        return util.exportExcel(list, "数据源配置");
    }

    /**
     * 根据数据源配置ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:query')")
    @GetMapping(value = "/{datasourceId}")
    public AjaxResult getInfo(@PathVariable("datasourceId") Long datasourceId)
    {
        return AjaxResult.success(datasourceService.selectDatasourceById(datasourceId));
    }

    /**
     * 新增数据源配置
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDatasource datasource)
    {
        if (Constants.NOT_UNIQUE.equals(datasourceService.checkDatasourceCodeUnique(datasource)))
        {
            return AjaxResult.error("新增数据源" + datasource.getDatasourceName() + "失败，数据源编码已存在");
        }
        datasource.setCreateBy(getUsername());
        return toAjax(datasourceService.insertDatasource(datasource));
    }

    /**
     * 修改数据源配置
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDatasource datasource)
    {
        if (Constants.NOT_UNIQUE.equals(datasourceService.checkDatasourceCodeUnique(datasource)))
        {
            return AjaxResult.error("修改数据源" + datasource.getDatasourceName() + "失败，数据源编码已存在");
        }
        datasource.setUpdateBy(getUsername());
        return toAjax(datasourceService.updateDatasource(datasource));
    }

    /**
     * 删除数据源配置
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:remove')")
    @DeleteMapping("/{datasourceIds}")
    public AjaxResult remove(@PathVariable Long[] datasourceIds)
    {
        return toAjax(datasourceService.deleteDatasourceByIds(datasourceIds));
    }

    /**
     * 测试数据源连接
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:add') || @ss.hasPermi('system:datasource:edit')")
    @PostMapping("/test")
    public AjaxResult testConnection(@RequestBody SysDatasource datasource)
    {
        boolean success = datasourceService.testDatasourceConnection(datasource);
        if (success)
        {
            return AjaxResult.success("连接成功");
        }
        else
        {
            return AjaxResult.error("连接失败，请检查配置信息");
        }
    }

    /**
     * 刷新动态数据源
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:refresh')")
    @PostMapping("/refresh")
    public AjaxResult refresh()
    {
        boolean success = datasourceService.refreshDynamicDatasource();
        if (success)
        {
            return AjaxResult.success("刷新成功");
        }
        else
        {
            return AjaxResult.error("刷新失败");
        }
    }

    /**
     * 获取数据源状态信息
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:status')")
    @GetMapping("/status/{datasourceCode}")
    public AjaxResult getStatus(@PathVariable("datasourceCode") String datasourceCode)
    {
        return AjaxResult.success(datasourceService.getDatasourceStatus(datasourceCode));
    }

    /**
     * 获取所有启用的数据源列表
     */
    @GetMapping("/select")
    public AjaxResult selectList()
    {
        List<SysDatasource> list = datasourceService.selectEnabledDatasource();
        return AjaxResult.success(list);
    }
}